<template>
    <div>
        <el-main class="main">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <div>
                    {{ $t('login.title') }}
                </div>
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')"> {{ $t('login.btnTitle') }}</el-button>
                    <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
                </el-form-item>
            </el-form>
        </el-main>
    </div>
</template>

<script>
import { mapActions } from 'vuex';
export default {
    name: 'Login',
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        return {
            ruleForm: {
                password: '',
                username: ''
            },
            rules: {
                password: [
                    { validator: validatePass, trigger: 'blur', required: true }
                ],
                username: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        ...mapActions('app', ['login']),
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.Login(this.ruleForm)
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        Login(params) {
            this.login(params)
            // this.$store.dispatch("app/login", params)
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    mounted() { },
}
</script>

<style  scoped>
.demo-ruleForm {
    width: 400px;
}

.main {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
</style>